package com.douxg.fastCoding.db;

import cn.hutool.core.util.StrUtil;
import com.jfinal.kit.StrKit;
import lombok.Data;

/**
 * @Description
 * @Auther douxg
 * @Date 2020/7/5 12:22
 */
@Data
public class ColumnsEO {
    private String tableName;
    private String columnName;
    private String columnDefault;
    private String isNullable;
    private String dataType;
    private Long characterMaximumLength;
    private String columnKey;
    private String columnComment;

    private String javaColumnName;
    private Boolean priKey;
    private DbDataType dbDataType;

    public Boolean getPriKey() {
        if (priKey == null) {
            priKey = "PRI".equals(getColumnKey());
        }
        return priKey;
    }

    public String getJavaDbDataType() {
        if (dbDataType == null) {
            return null;
        }
        return dbDataType.getJavaDbDataType().getType();
    }

    public String getJavaColumnName() {
        if (javaColumnName == null) {
            javaColumnName = StrKit.toCamelCase(getColumnName(), true);
        }
        return javaColumnName;
    }

    public String getJavaColumnFmtName() {
        return nameHandle("", "Fmt", "javaColumnName");
    }

    public String getStartFmtName() {
        return nameHandle("start", "Fmt", "javaColumnName");
    }

    public String getEndFmtName() {
        return nameHandle("end", "Fmt", "javaColumnName");
    }

    public String getStartName() {
        return nameHandle("start", "", "javaColumnName");
    }

    public String getEndName() {
        return nameHandle("end", "", "javaColumnName");
    }

    public String getMybatisDataType() {
        if (dbDataType == null) {
            return null;
        }
        return dbDataType.getMybatisJdbcType().name();
    }

    public String getGetterName(String type) {
        return nameHandle("get", "", type);
    }

    public String getSetterName(String type) {
        return nameHandle("set", "", type);
    }

    public String nameHandle(String prefix, String suffix, String type) {
        String name = null;
        if ("javaColumnName".equals(type)) {
            name = getJavaColumnName();
        } else if ("javaColumnFmtName".equals(type)) {
            name = getJavaColumnFmtName();
        } else if ("startFmtName".equals(type)) {
            name = getStartFmtName();
        } else if ("endFmtName".equals(type)) {
            name = getEndFmtName();
        } else if ("startName".equals(type)) {
            name = getStartName();
        } else if ("endName".equals(type)) {
            name = getEndName();
        }
        if (name != null) {
            if (StrUtil.isNotBlank(prefix)) {
                name = StrKit.firstCharToUpperCase(name);
            }
            name = prefix + name + suffix;
        }
        return name;
    }
}
